home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / demos / polarViewDemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  9.4 KB  |  392 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*  polarViewDemo.c - Show the effect of changes in the incidence and
  19.  *      azimuth paramters in the polarView routine.
  20.  *
  21.  *  Left Mousebutton        - change azimuth angle
  22.  *  Middle Mousebuttonn        - change incidence angle
  23.  *  Right Mousebutton        - change the twist angle
  24.  *  F1 key            - print help information
  25.  *  R key                       - reset incidence, azimuth, and twist of eye
  26.  *  Escape key                  - exit the program
  27.  */
  28. #include <GL/gl.h>
  29. #include <GL/glu.h>
  30. #include <GL/glut.h>
  31. #include <math.h>
  32. #include <stdio.h>    /* for printf */
  33.  
  34. /*  Function Prototypes  */
  35.  
  36. GLvoid  initgfx( GLvoid );
  37. GLvoid  drawScene( GLvoid );
  38. GLvoid  reshape( GLsizei, GLsizei );
  39. GLvoid  keyboard( GLubyte, GLint, GLint );
  40. GLvoid  specialkeys( GLint, GLint, GLint );
  41. GLvoid  mouse( GLint, GLint, GLint, GLint );
  42. GLvoid  motion( GLint, GLint );
  43.  
  44. void resetView( GLvoid );
  45. void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
  46.  
  47. void printHelp( char * );
  48.  
  49. /* Global Definitions */
  50.  
  51. #define KEY_ESC    27    /* ascii value for the escape key */
  52.  
  53. /* Global Variables */
  54.  
  55. static char         *progname;
  56. static void           *fixedFont;
  57.  
  58. static GLdouble        xStart = 0.0, yStart = 0.0;
  59.  
  60. static GLfloat         near, far, distance, twistAngle, incAngle, azimAngle;
  61.  
  62. /* action values */
  63.  
  64. static enum    actions { CHANGE_AZIM, CHANGE_INC, CHANGE_TWIST, CHANGE_NONE };
  65. static GLint    action = CHANGE_AZIM;
  66.  
  67. void
  68. main( int argc, char *argv[] )
  69. {
  70.     GLsizei width, height;
  71.  
  72.     glutInit( &argc, argv );
  73.  
  74.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  75.     height = glutGet( GLUT_SCREEN_HEIGHT );
  76.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  77.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  78.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  79.     glutCreateWindow( argv[0] );
  80.  
  81.     initgfx();
  82.  
  83.     glutMouseFunc( mouse );
  84.     glutMotionFunc( motion );
  85.     glutKeyboardFunc( keyboard );
  86.     glutSpecialFunc( specialkeys );
  87.     glutReshapeFunc( reshape );
  88.     glutDisplayFunc( drawScene ); 
  89.  
  90.     progname = argv[0];
  91.     printHelp( argv[0] );
  92.  
  93.     glutMainLoop();
  94. }
  95.  
  96. void
  97. printHelp( char *progname )
  98. {
  99.     fprintf(stdout, "\n%s - combine modeling transformations to \n" 
  100.     "view objects as though encased in a glass ball\n\n"
  101.     "Axes: X - red, Y - green, Z - blue\n\n"
  102.     "Left Mousebutton       - change azimuth angle\n"
  103.     "Middle Mousebuttonn    - change incidence angle\n"
  104.     "Right Mousebutton      - change the twist angle\n" 
  105.     "R key                  - reset incidence, azimuth, and twist angles\n"
  106.     "F1 Key            - print Help information\n"
  107.     "Escape key             - exit the program\n",
  108.     progname);
  109. }
  110.  
  111. GLvoid
  112. initgfx( GLvoid )
  113. {
  114.     GLfloat   yellowAmbient[] = { 1.0, 1.0, 0.0, 1.0 };
  115.     GLfloat   yellowDiffuse[] = { 1.0, 1.0, 0.0, 1.0 };
  116.     GLfloat   whiteSpecular[] = { 1.0, 1.0, 1.0, 1.0 };
  117.     
  118.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  119.     glEnable( GL_DEPTH_TEST );
  120.  
  121.     /* Set up near and far so that ( far - near ) > maxObjectSize, */
  122.     /* and determine the viewing distance (adjust for zooming) */
  123.     near = 2.5;
  124.     far = 7.0;
  125.  
  126.     resetView();
  127.  
  128.     glMaterialfv( GL_FRONT, GL_AMBIENT, yellowAmbient );
  129.     glMaterialfv( GL_FRONT, GL_DIFFUSE, yellowDiffuse );
  130.     glMaterialfv( GL_FRONT, GL_SPECULAR, whiteSpecular );
  131.     glMaterialf( GL_FRONT, GL_SHININESS, 100.0 );
  132.  
  133.     /* Turn on a default light */
  134.     glEnable( GL_LIGHT0 );
  135.  
  136.     /* Set up fonts to use for rendering */
  137.     fixedFont = GLUT_BITMAP_9_BY_15;
  138. }
  139.  
  140. GLvoid
  141. reshape( GLsizei width, GLsizei height )
  142. {
  143.     GLdouble            aspect;
  144.  
  145.     glViewport( 0, 0, width, height );
  146.  
  147.     aspect = (GLdouble) width / (GLdouble) height;
  148.  
  149.     glMatrixMode( GL_PROJECTION );
  150.     glLoadIdentity();
  151.     gluPerspective( 47.0, aspect, near, far );
  152.     glMatrixMode( GL_MODELVIEW );
  153. }
  154.  
  155. GLvoid 
  156. keyboard( GLubyte key, GLint x, GLint y )
  157. {
  158.     switch (key) {
  159.     case 'R':
  160.         resetView();
  161.         glutPostRedisplay();
  162.         break;
  163.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  164.         exit(0);
  165.     }
  166. }
  167.  
  168. GLvoid 
  169. specialkeys( GLint key, GLint x, GLint y )
  170. {
  171.     switch (key) {
  172.     case GLUT_KEY_F1:    /* print Help */
  173.         printHelp( progname );
  174.         break;
  175.     }
  176. }
  177.  
  178. GLvoid 
  179. mouse( GLint button, GLint state, GLint x, GLint y )
  180. {
  181.     if (state == GLUT_DOWN) {
  182.         switch (button) {
  183.         case GLUT_LEFT_BUTTON: 
  184.             action = CHANGE_AZIM;
  185.             break;
  186.         case GLUT_MIDDLE_BUTTON: 
  187.             action = CHANGE_INC;
  188.             break;
  189.         case GLUT_RIGHT_BUTTON: 
  190.             action = CHANGE_TWIST;
  191.             break;
  192.         }
  193.  
  194.         /* Update the saved mouse position */
  195.         xStart = x;
  196.         yStart = y;
  197.     } else {
  198.         action = CHANGE_NONE;
  199.     }
  200. }
  201.  
  202. GLvoid
  203. motion( GLint x, GLint y )
  204. {
  205.     switch (action) {
  206.     case CHANGE_AZIM:
  207.         /* Adjust the eye position based on the mouse position */
  208.         azimAngle += (GLdouble) (x - xStart);
  209.         break;
  210.     case CHANGE_INC:
  211.         /* Adjust the eye position based on the mouse position */
  212.         incAngle -= (GLdouble) (y - yStart);
  213.         break;
  214.     case CHANGE_TWIST:
  215.         /* Adjust the eye twist based on the mouse position */
  216.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  217.         break;
  218.     default:
  219.         printf("unknown action %d\n", action);
  220.     }
  221.     
  222.     /* Update the stored mouse position for later use */
  223.     xStart = x;
  224.     yStart = y;
  225.  
  226.     glutPostRedisplay();
  227. }
  228.  
  229. void
  230. resetView( GLvoid )
  231. {
  232.     distance = near + (far - near) / 2.0;
  233.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  234.     incAngle = 0.0;
  235.     azimAngle = 0.0;
  236. }
  237.  
  238. void
  239. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  240.             GLfloat twist)
  241. {
  242.     glTranslatef( 0.0, 0.0, -distance);
  243.     glRotatef( -twist, 0.0, 0.0, 1.0);
  244.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  245.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  246. }
  247.  
  248. GLvoid
  249. drawNormal( GLfloat px, GLfloat py, GLfloat pz,
  250.     GLfloat nx, GLfloat ny, GLfloat nz )
  251. {
  252.     glPushAttrib( GL_CURRENT_BIT | GL_LINE_BIT | GL_ENABLE_BIT );
  253.       glDisable( GL_LIGHTING );
  254.       glColor3f( 1.0, 1.0, 1.0 );
  255.       glLineWidth( 2.5 );
  256.       glPushMatrix();
  257.         glTranslatef( px, py, pz );
  258.         glBegin( GL_LINES );
  259.           glVertex3f( 0.0, 0.0, 0.0 );
  260.           glVertex3f( nx, ny, nz );
  261.         glEnd();
  262.       glPopMatrix();
  263.     glPopAttrib();
  264. } /* drawNormal */
  265.  
  266. GLvoid
  267. renderBitmapString( void *font, char *string )
  268. {
  269.         int i;
  270.         int len = (int) strlen(string);
  271.         for (i = 0; i < len; i++) {
  272.                 glutBitmapCharacter(font, string[i]);
  273.         }
  274. }
  275.  
  276. GLvoid
  277. drawStatus( GLvoid )
  278. {
  279.     static char a[50];
  280.     GLsizei winWidth, winHeight;
  281.  
  282.     /* save the ModelView matrix */
  283.     glPushMatrix(); 
  284.  
  285.     /* clear any current modeling or viewing xforms */
  286.     glLoadIdentity(); 
  287.  
  288.  
  289.     /* save the Projection matrix */
  290.     glMatrixMode( GL_PROJECTION );
  291.     glPushMatrix();
  292.  
  293.         /* make new world space to display status in */
  294.         glLoadIdentity();
  295.         winWidth = glutGet(GLUT_WINDOW_WIDTH); 
  296.         winHeight = glutGet(GLUT_WINDOW_HEIGHT);
  297.         glOrtho( -0.5, winWidth-0.5, -0.5, winHeight-0.5, -1, 1.0 );
  298.  
  299.         glPushAttrib( GL_DEPTH_BUFFER_BIT );
  300.     
  301.             /* don't want text to be hidden */
  302.             glDisable( GL_DEPTH_TEST ); 
  303.  
  304.             glColor3f( 1.0, 1.0, 0.0 );
  305.  
  306.             glRasterPos3f( 10.0, winHeight-15.0, 0.0 );
  307.             sprintf (a, "left mouse - change azimuth");
  308.             renderBitmapString( fixedFont, a );
  309.  
  310.             glRasterPos3f( 10.0, winHeight-35.0, 0.0 );
  311.             sprintf (a, "middle mouse - change incidence");
  312.             renderBitmapString( fixedFont, a );
  313.  
  314.             glRasterPos3f( 10.0, winHeight-55.0, 0.0 );
  315.             sprintf (a, "right mouse - change twist");
  316.             renderBitmapString( fixedFont, a );
  317.  
  318.             glRasterPos3f( 10.0, winHeight-75.0, 0.0 );
  319.             sprintf (a, "R key - reset view");
  320.             renderBitmapString( fixedFont, a );
  321.  
  322.             /* draw at pixel coord 10, 65 */
  323.             glRasterPos3f( 10.0, 65.0, 0.0 ); 
  324.             sprintf (a, "azimuth is %4d", (GLint) azimAngle);
  325.             renderBitmapString( fixedFont, a );
  326.  
  327.             glRasterPos3f( 10.0, 40.0, 0.0 );
  328.             sprintf (a, "incidence is  %4d", (GLint) incAngle);
  329.             renderBitmapString( fixedFont, a );
  330.  
  331.             glRasterPos3f( 10.0, 15.0, 0.0 );
  332.             sprintf (a, "Twist is %4d", (GLint) twistAngle);
  333.             renderBitmapString( fixedFont, a );
  334.     
  335.         glPopAttrib();
  336.  
  337.     /* restore matrixes */
  338.     glPopMatrix();
  339.  
  340.     glMatrixMode( GL_MODELVIEW );
  341.     glPopMatrix();
  342. } /* drawStatus */
  343.  
  344. GLvoid
  345. drawScene( GLvoid )
  346. {
  347.     GLfloat   lightPosition0[] = { 1.0, 1.0, 1.0, 0.0 };
  348.  
  349.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  350.  
  351.     glPushMatrix();
  352.  
  353.       glLightfv( GL_LIGHT0, GL_POSITION, lightPosition0 );
  354.  
  355.       polarView( distance, azimAngle, incAngle, twistAngle );
  356.  
  357.       /* Turn on lighting computations */
  358.       glEnable( GL_LIGHTING );
  359.     
  360.       /* Draw a lit torus rotated 60 degrees about the x axis */
  361.       glPushMatrix();
  362.         glRotatef( 60.0, 1.0, 0.0, 0.0 );
  363.         glutSolidTorus( 0.3, 0.6, 15, 15 );
  364.  
  365.         /* Turn off lighting comptations */
  366.         glDisable( GL_LIGHTING );
  367.  
  368.         /* Draw a line ( unlit ) representing the face normal */
  369.         drawNormal( 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 );
  370.       glPopMatrix();
  371.  
  372.       XYZaxes();
  373.  
  374.       glColor3f( 0.3, 0.8, 0.8 );
  375.       glutWireSphere( 1.5, 15, 15 );
  376.  
  377.       /* label north and south poles */
  378.       glColor3f( 1.0, 0.0, 0.0 );
  379.       glRasterPos3f( 0.0, 0.0, -1.6 );
  380.       renderBitmapString( fixedFont, "South Pole" );
  381.  
  382.       /* glColor3f( 1.0, 0.0, 0.0 ); */
  383.       glRasterPos3f( 0.0, 0.0, 1.55 );
  384.       renderBitmapString( fixedFont, "North Pole" );
  385.  
  386.     glPopMatrix();
  387.  
  388.     drawStatus();
  389.  
  390.     glutSwapBuffers();
  391. }
  392.